/*
 * 础石AUTOSAR CP操作系统
 *
 * Copyright (C) 2022 国科础石(重庆)软件有限公司
 *
 * 作者: Yongbo Zhang <zhangyongbo@kernelsoft.com>
 *
 * 基于linux/drivers/base/base.h
 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
 * Copyright (c) 2008-2012 Novell Inc.
 * Copyright (c) 2012-2019 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
 * Copyright (c) 2012-2019 Linux Foundation
 *
 * License terms: GNU General Public License (GPL) version 3
 *
 */

#ifndef __DRIVERS_BASE_H
#define __DRIVERS_BASE_H

#include <dim-sum/string.h>

#define find_by_name(_name, found, pos, head, member)		\
	list_for_each_entry(pos, head, member) {	\
		if (!strcmp(pos->name, _name)) {	\
			found = pos;			\
			break;				\
		}					\
	}

#define for_each_do_fn(fn, data, ret, pos, head, member)	\
	list_for_each_entry(pos, head, member) {	\
		BUG(); \
		ret = fn(pos, data);			\
		if (ret)				\
			break;				\
	}

#define find_dev_by_dt_node(dt_ndoe, found, pos, head, member)		\
	list_for_each_entry(pos, head, member) {	\
		if (dt_ndoe == pos->dt_node) {	\
			found = pos;			\
			break;				\
		}					\
	}
/**
 * 添加设备到总线上
 */
int bus_add_device(struct device *dev);
/**
 * 从总线上移除设备
 */
void bus_remove_device(struct device *dev);
/**
 * 添加驱动到总线上
 */
int bus_add_driver(struct device_driver *drv);
/**
 * 尝试将设备连接到某一个驱动上
 */
int device_attach(struct device *dev);
/**
 * 释放设备
 */
void device_release(struct device *dev);

/**
 * 手动将设备从驱动中分离出来
 */
void device_release_driver(struct device *dev);

/* 设备驱动子系统初始化函数 */
int buses_init(void);
int classes_init(void);
int platform_bus_init(void);
/**
 * 根据DTB创建平台设备
 */
int dt_platform_device_create(struct device_node *node);

#endif /* __DRIVERS_BASE_H */

